package com.gamerent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gamerent.common.ResponseResult;
import com.gamerent.entity.GameAccount;
import com.gamerent.service.GameAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 游戏账号控制器 - 合并了原来的商品服务功能
 */
@Slf4j
@RestController
@RequestMapping("/api/accounts")
public class GameAccountController {

    @Autowired
    private GameAccountService gameAccountService;

    /**
     * 分页查询游戏账号
     */
    @GetMapping
    public ResponseResult<IPage<GameAccount>> findByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String gameName,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Integer status) {

        Page<GameAccount> page = new Page<>(current, size);
        IPage<GameAccount> result = gameAccountService.findByPage(page, gameName, categoryId, status);
        return ResponseResult.success("查询成功", result);
    }

    /**
     * 根据ID查询游戏账号
     */
    @GetMapping("/{id}")
    public ResponseResult<GameAccount> findById(@PathVariable Long id) {
        GameAccount gameAccount = gameAccountService.findById(id);
        if (gameAccount != null) {
            return ResponseResult.success("查询成功", gameAccount);
        } else {
            return ResponseResult.error(404, "游戏账号不存在");
        }
    }

    /**
     * 创建游戏账号
     */
    @PostMapping
    public ResponseResult<GameAccount> create(@RequestBody GameAccount gameAccount) {
        try {
            GameAccount created = gameAccountService.create(gameAccount);
            return ResponseResult.success("创建成功", created);
        } catch (Exception e) {
            log.error("创建游戏账号失败: {}", e.getMessage());
            return ResponseResult.error("创建失败: " + e.getMessage());
        }
    }

    /**
     * 更新游戏账号
     */
    @PutMapping("/{id}")
    public ResponseResult<GameAccount> update(@PathVariable Long id, @RequestBody GameAccount gameAccount) {
        try {
            gameAccount.setId(id);
            GameAccount updated = gameAccountService.update(gameAccount);
            return ResponseResult.success("更新成功", updated);
        } catch (Exception e) {
            log.error("更新游戏账号失败: {}", e.getMessage());
            return ResponseResult.error("更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除游戏账号
     */
    @DeleteMapping("/{id}")
    public ResponseResult<Void> delete(@PathVariable Long id) {
        try {
            boolean deleted = gameAccountService.delete(id);
            if (deleted) {
                return ResponseResult.success("删除成功");
            } else {
                return ResponseResult.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除游戏账号失败: {}", e.getMessage());
            return ResponseResult.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 获取热门游戏账号
     */
    @GetMapping("/hot")
    public ResponseResult<List<GameAccount>> findHotAccounts(@RequestParam(defaultValue = "10") Integer limit) {
        List<GameAccount> hotAccounts = gameAccountService.findHotAccounts(limit);
        return ResponseResult.success("查询成功", hotAccounts);
    }

    /**
     * 搜索游戏账号
     */
    @GetMapping("/search")
    public ResponseResult<IPage<GameAccount>> search(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String gameName,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice) {

        Page<GameAccount> page = new Page<>(current, size);
        IPage<GameAccount> result = gameAccountService.search(page, keyword, gameName, categoryId, minPrice, maxPrice);
        return ResponseResult.success("搜索成功", result);
    }

    /**
     * 更新账号状态
     */
    @PostMapping("/status/{id}")
    public ResponseResult<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            GameAccount gameAccount = gameAccountService.findById(id);
            if (gameAccount == null) {
                return ResponseResult.error(404, "账号不存在");
            }

            gameAccount.setStatus(status);
            gameAccountService.update(gameAccount);

            return ResponseResult.success("状态更新成功");
        } catch (Exception e) {
            log.error("更新账号状态失败: {}", e.getMessage());
            return ResponseResult.error("更新失败: " + e.getMessage());
        }
    }
}